package com.qf.lanzhou.shop.admin.controller;

import com.qf.lanzhou.shop.pojo.entity.Seller;
import com.qf.lanzhou.shop.pojo.vo.BaseRespVO;
import com.qf.lanzhou.shop.admin.service.impl.SellerServiceImpl;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * @author jack
 */
@Api(tags = "卖家登录接口")
@ResponseBody
@RestController
public class SellerController extends BaseController {

    @Autowired
    private SellerServiceImpl sellerService;


    @ApiOperation(value = "卖家登录接口")
    @GetMapping(value = "/login")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "sellerName", value = "卖家名",
                    required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password", value = "卖家密码",
                    required = true, dataType = "String"),
    })
    public BaseRespVO sellerLogin(String sellerName, String password, HttpServletRequest req) {
        Seller seller = sellerService.getSeller(sellerName);
        if (seller == null) {
            return convertErrorResult("卖家用户不存在");
        }
        if (!password.equals(seller.getPassword())) {
            return convertErrorResult("密码不正确");
        }
        req.getSession().setAttribute("seller", seller);
        return convertSuccessResult(seller);
    }

    @ApiOperation(value = "注册接口")
    @PostMapping(value = "/registration")
    public BaseRespVO sellerRegistration(Seller seller, HttpServletRequest req) {
        System.out.println("registration");
        Seller seller1 = sellerService.getSeller(seller.getSellerName());
        if (seller1 != null) {
            System.out.println("用户已经存在");
            return convertErrorResult("用户已经存在，请重新输入");
        }
        System.out.println("用户不存在 准备插入");
        boolean b = sellerService.addSeller(seller);
        if (b) {
            return convertErrorResult("服务器繁忙");
        }
        return convertSuccessResult("注册成功成功");
    }

    @PostMapping(value = "/upload")
    @ApiOperation("头像上传")
    public BaseRespVO avatar(MultipartFile file, HttpServletRequest req) {
        System.out.println("头像上传日志");
        System.out.println(file);
        if (file.isEmpty()) {
            throw new RuntimeException("文件上传失败");
        }
        //获取文件目录
        String dir = "D:\\千峰练习代码\\自己码\\project3\\views\\view-admin\\assets\\images\\avatar";
        String fileName = dir;
        //判断目录是否存在，不存在创建
        File file1 = new File(fileName);
        System.out.println("头像上传日志2");
        if (file1.exists()) {
            file1.mkdirs();
            System.out.println("创建目录");
        }
        System.out.println("头像上传日志3");
        //截取文件后缀
        String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),
                file.getOriginalFilename().length());
        //获取文件名
        String fileName1 = System.currentTimeMillis() + fileSuffix;
        System.out.println(fileName1);
        //保存文件
        String filePath = fileName + File.separator + fileName1;

        File saveFile = new File(filePath);
        System.out.println(filePath);
        try {
            HttpSession session = req.getSession();
            Seller seller = (Seller) session.getAttribute("seller");
            if (seller == null) {
                return convertErrorResult("请先登录");
            }
            int i = sellerService.upload(seller.getSellerName(), fileName1);
            if (i != 1) {
                return convertErrorResult("上传失败");
            }
            file.transferTo(saveFile);
            System.out.println("上传成功");
        } catch (IOException e) {
            throw new RuntimeException("文件上传失败");
        }
        return convertSuccessResult("成功");
    }

    @GetMapping(value = "/getAvatar")
    @ApiOperation(value = "获取头像接口")
    public BaseRespVO getAvatar(HttpServletRequest req) {
        Seller seller = (Seller) req.getSession().getAttribute("seller");
        if (seller == null) {
            return convertSuccessResult("1610176679728.jpg");
        }
        String avatar = sellerService.getAvatar(seller.getSellerName());
        return convertSuccessResult(avatar);
    }

    @ApiOperation(value = "退出登录接口")
    public BaseRespVO outLogin(HttpServletRequest req) {
        HttpSession session = req.getSession();
        session.removeAttribute("seller");
        return convertSuccessResult("退出登录成功，请从新登录");
    }
}
